<?php
namespace app\admin\controller;

use app\admin\model\Admin;
use app\common\model\User as UserModel;
use think\Controller;
use think\Db;
use think\Request;

class User extends Common
{
    public function index()
    {
    	if(Request::instance()->isAjax()){

    		$where = [];

    		$data = input('');


    		if(input('nickname')){
    			$where['nickname'] = ['like','%'.input('nickname').'%'];
    		}

    		
    		$start = input('start') ? input('start') : "1970-01-01";

    		$end = input('end') ? input('end') : date("Y-m-d H:i:s");

    		$where['create_time'] = [['>=',strtotime($start)],['<=',strtotime($end)+86400]];

    		$users = UserModel::where($where)->paginate($data['limit']);

    		// var_dump();exit;

    		exit(json_encode(['code'=>0,'count'=>$users->total(),'data'=>$users->items()]));

    	}else{
    		return $this->fetch();
    	}
    }

    public function changestatus()
    {
    	// $this->error("提交失败");
    	$user_id = input('user_id');

    	$user = UserModel::get($user_id);

    	if($user->status==1){
    		$user->status = 0;
    	}else{
    		$user->status = 1;
    	}

    	$res = $user->save();

    	if($res){
    		$this->success("提交成功");
    	}else{
    		$this->error("提交失败");
    	}
    }

    public function del()
    {
    	$user_id = input('user_id');

    	$res = UserModel::destroy($user_id);

    	if($res){
    		$this->success("提交成功");
    	}else{
    		$this->error("提交失败");
    	}
    }

    public function edit()
    {
    	$user_id = input('user_id');

    	$user = UserModel::get($user_id);


    	$this->assign('user',$user);

    	return $this->fetch();

    }
}